import CryptoJS from 'crypto-js'

const SECRET_KEY = 'Mw7DJgHqUZFAamxf8pXPjs28aBX1Ko8X'

// AES加密
export const encryptAES = data => {
  const keyHex = CryptoJS.enc.Utf8.parse(SECRET_KEY)
  const option = { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }
 let srcs = CryptoJS.enc.Utf8.parse(data);
  const encrypted = CryptoJS.AES.encrypt(srcs, keyHex, option)
  return encrypted.toString()
}

// export function encryptByAES(word, keyStr) {
//     keyStr = keyStr ? keyStr : ''; //判断是否存在ksy，不存在就用定义好的key
//     let key = CryptoJS.enc.Utf8.parse(keyStr);
//     let srcs = CryptoJS.enc.Utf8.parse(word);
//     let encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
//     return encrypted.toString();    
// }



// AES解密
export const decryptAES = data => {
  const keyHex = CryptoJS.enc.Utf8.parse(SECRET_KEY)
  const decrypted = CryptoJS.AES.decrypt(data, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return decrypted.toString(CryptoJS.enc.Utf8)
}